package cn.zwx.search.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.zwx.search.common.ResponseMsg;
import cn.zwx.search.common.enums.RetCode;
import cn.zwx.search.exceptioin.BizException;
import cn.zwx.search.model.es.ESProduct;
import cn.zwx.search.model.req.DelDocReq;
import cn.zwx.search.model.req.FindDocByIdReq;
import cn.zwx.search.service.ProductService;
import cn.zwx.search.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 商品索引Api
 * @projectName:search-example
 * @see:cn.zwx.search.controller
 * @author:zhangwenxue
 * @createTime:2020/10/24 13:22
 * @version:1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {
      @Autowired
      private ProductService productService;


    /**
     * @description 商品创建索引
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @RequestMapping(value = "/create/index",method = RequestMethod.POST)
    public ResponseMsg createIndex(@RequestBody List<ESProduct> productList){

        productService.createIndex(productList);

        return ResponseMsg.success();
    }

    /**
     * @description 根据id查询文档信息
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @RequestMapping(value = "/find/by/id",method = RequestMethod.GET)
    public ResponseMsg findDocById(@RequestBody FindDocByIdReq req){

        ValidatorUtils.validateEntity(req);

        ESProduct docById = productService.findDocById(req);

        return ResponseMsg.success(docById);
    }


    /**
     * @description 更新文档
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @RequestMapping(value = "/update/doc",method = RequestMethod.POST)
    public ResponseMsg<ESProduct> updateDoc(@RequestBody List<ESProduct> proList){
        if (CollectionUtil.isEmpty(proList)){
            throw new BizException(RetCode.PARAM_ERROR.getCode(),"请求参数不能为空");
        }
         productService.updateDoc(proList);

        return ResponseMsg.success();
    }


    /**
     * @description 删除文档
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @RequestMapping(value = "/del/doc",method = RequestMethod.POST)
    public ResponseMsg delDoc(@RequestBody DelDocReq req){
        productService.delDoc(req);
        return ResponseMsg.success();
    }


    /**
     * @description 删除索引
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2020/10/23 15:55
     **/
    @RequestMapping(value = "/del/index",method = RequestMethod.GET)
    public ResponseMsg delIndex( String index){
        productService.delIndex(index);
        return ResponseMsg.success();
    }
}
